/* 用户的增删改查 */
const   router = require('koa-router')(),
        DB = require("../../module/db.js")

router.get('/',async(ctx) =>{
    await ctx.render('admin/index')
})
router.get('/remove',async(ctx) =>{
    try{
        var collection = ctx.query.collection /* 数据库表 */
        var id = ctx.query.id /* id删除的 */
        console.log(collection + '~~~~~~~~~~~~~~~~~')
        console.log(id + '~~~~~~~~~~~~~~~~~')
        var result = DB.remove(collection,{'_id':DB.getObjectId(id)})
        // 返回到哪里？
        ctx.redirect(ctx.state.G.prevPage)
    }catch(err){
        ctx.redirect(ctx.state.G.prevPage)
    }
})
router.get('/changeStatus',async(ctx) =>{
    var collectionName = ctx.query.collectionName
    var attr = ctx.query.attr
    var id = ctx.query.id
    var data = await DB.find(collectionName,{'_id':DB.getObjectId(id)})
    if(data.length > 0){
        if(data[0][attr] == 1){
            var json = {
                [attr]:0
            }
        }else{
            var json = {
                [attr]:1
            }
        }
        let updateResult = await DB.update(collectionName,{'_id':DB.getObjectId(id)},json)
        if(updateResult){
            ctx.body = {"msg":"更新成功","success":true}
        }else{
            ctx.body = {"msg":"更新失败","success":false}
        }
    }else{
        ctx.body = {"msg":"更新失败，参数错误","success":false}
    }
  
})
/* router.get('/edit',async(ctx) =>{
   // ctx.body = '用户首页'
   await ctx.render('admin/user/edit')
})
router.get('/del',async(ctx) =>{
    ctx.body = '用户删除'
}) */
module.exports = router.routes()